package com.keai.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.keai.dao.MemberDao;
import com.keai.dao.OrderDao;
import com.keai.dao.SetmealDao;
import com.keai.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    MemberDao memberDao;

    @Autowired
    OrderDao orderDao;

    @Autowired
    SetmealDao setmealDao;


    /**
     reportDate:   统计日期（今天）
     todayNewMember :0,  今日新增会员数
     totalMember :0,     会员总数
     thisWeekNewMember :0,   本周新增会员数（周一以后新增了多少会员）
     thisMonthNewMember :0,  本月新增会员数（1号以后新增了多少会员）
     todayOrderNumber :0,    今日预约数（预约今天来体检的人数）
     todayVisitsNumber :0,   今日到诊数（今天实际有多少人体检）
     thisWeekOrderNumber :0, 本周预约数（预约本周（周一~~~周日）来体检的人数）
     thisWeekVisitsNumber :0, 本周到诊数（本周（周一以后）实际来了多少人）
     thisMonthOrderNumber :0,  本月预约数（预约本月（1号--月底）来体检的人数）
     thisMonthVisitsNumber :0, 本月到诊数(本月1号以后实际来了多少人)
     hotSetmeal （热门套餐）:[
     {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
     {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
     ]
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //今天日期
        String todayDate = DateUtils.parseDate2String(new Date());
        //获取本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本周日的日期
        String thisWeekSunday = DateUtils.parseDate2String(DateUtils.getSundayOfThisWeek());
        //获取本月1号
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //获取本月底
        String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getLastDay4ThisMonth());

        //统计日期
        String reportDate = todayDate;
        //todayNewMember :  今日新增会员数
        long todayNewMember = memberDao.findTodayNewMember(todayDate);
        //totalMember :    会员总数
        long totalMember = memberDao.findTotalMember();
        //thisWeekNewMember :0,   本周新增会员数（周一以后新增了多少会员）
        long thisWeekNewMember = memberDao.findNewMemberCountAfterByDate(thisWeekMonday);
        //thisMonthNewMember :0,  本月新增会员数（1号以后新增了多少会员）
        long thisMonthNewMember = memberDao.findNewMemberCountAfterByDate(thisMonthFirstDay);
        //todayOrderNumber :0,    今日预约数（预约今天来体检的人数）
        long todayOrderNumber = orderDao.findTodayOrderNumber(todayDate);
        // todayVisitsNumber :0,   今日到诊数（今天实际有多少人体检）
        long todayVisitsNumber = orderDao.findVisitsNumber(todayDate);
        //thisWeekOrderNumber :0, 本周预约数（预约本周（周一~~~周日）来体检的人数）
        long thisWeekOrderNumber = orderDao.findOrderNumberByDate(thisWeekMonday,thisWeekSunday);
        //thisWeekVisitsNumber :0, 本周到诊数（本周（周一以后）实际来了多少人）
        long thisWeekVisitsNumber = orderDao.findVisitsAfterDate(thisWeekMonday);
        //thisMonthOrderNumber :0,  本月预约数（预约本月（1号--月底）来体检的人数）
        long thisMonthOrderNumber = orderDao.findOrderNumberByDate(thisMonthFirstDay, thisMonthLastDay);
        //thisMonthVisitsNumber :0, 本月到诊数(本月1号以后实际来了多少人)
        long thisMonthVisitsNumber = orderDao.findVisitsAfterDate(thisMonthFirstDay);
        /**
         *  hotSetmeal （热门套餐）(取预约最多的两个):[
         {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
         {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
         ]
         */
        List<Map<String,Object>> mapList = setmealDao.findHotSetmeal();

//        整合数据
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("reportDate",reportDate);
        resultMap.put("todayNewMember",todayNewMember);
        resultMap.put("totalMember",totalMember);
        resultMap.put("thisWeekNewMember",thisWeekNewMember);
        resultMap.put("thisMonthNewMember",thisMonthNewMember);
        resultMap.put("todayOrderNumber",todayOrderNumber);
        resultMap.put("todayVisitsNumber",todayVisitsNumber);
        resultMap.put("thisWeekOrderNumber",thisWeekOrderNumber);
        resultMap.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        resultMap.put("thisMonthOrderNumber",thisMonthOrderNumber);
        resultMap.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        resultMap.put("hotSetmeal",mapList);

        return resultMap;
    }
}
